//
//  TeachManagementSearchView.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/9/26.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "TeachManagementSearchView.h"
@interface TeachManagementSearchView()<UITextFieldDelegate>
@property (nonatomic,weak) UITextField *searchTextField;
@property (nonatomic,weak) UIButton *serachBtn;
@property (nonatomic,assign) NSInteger num;
@end

@implementation TeachManagementSearchView

- (instancetype)initWithFrame:(CGRect)frame WithZjkmNum:(NSInteger)zjkmNum{
    self = [super initWithFrame:frame];
    if (self) {
        self.num = zjkmNum;
        [self createSubView];
    }
    return self;
}

#pragma mark ==========创建视图==========
- (void)createSubView{
    UIColor *backLor = [UIColor colorWithHexString:@"#eeeeee"];
    UITextField *searchTextField = [[UITextField alloc]init];
    searchTextField.backgroundColor = backLor;
    searchTextField.placeholder = @"请输入学员姓名";
    [searchTextField setValue:[UIFont boldSystemFontOfSize:LoginFont-3] forKeyPath:@"_placeholderLabel.font"];
    searchTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30*SCALEUNIT, 0)];;
    searchTextField.leftViewMode = UITextFieldViewModeAlways;
    searchTextField.font = [UIFont boldSystemFontOfSize:LoginFont-3];
    searchTextField.textColor = [UIColor colorWithHexString:@"#999999"];
    [searchTextField setClearsOnBeginEditing:NO];
    [searchTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 630*SCALEUNIT, 65*SCALEUNIT) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10*SCALEUNIT,10*SCALEUNIT)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.path = maskPath.CGPath;
    searchTextField.layer.mask = maskLayer;
    searchTextField.delegate = self;
    _searchTextField = searchTextField;
    [self addSubview:self.searchTextField];
    
    UIButton *serachBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [serachBtn setBackgroundImage:[UIImage imageNamed:@"TeachManagementSearchImage"] forState:UIControlStateNormal];
    [serachBtn addTarget:self action:@selector(searchOpen:) forControlEvents:UIControlEventTouchUpInside];
    _serachBtn = serachBtn;
    [self addSubview:self.serachBtn];
    //布局
    WS(ws);
    [self.searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws).with.offset(30*SCALEUNIT);
        make.left.mas_equalTo(ws).with.offset(30*SCALEUNIT);
        make.size.mas_equalTo(CGSizeMake(630*SCALEUNIT, 65*SCALEUNIT));
    }];
    [self.serachBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.searchTextField.mas_right).with.offset(25*SCALEUNIT);
        make.centerY.mas_equalTo(ws.searchTextField);
        make.size.mas_equalTo(CGSizeMake(32*SCALEUNIT, 32*SCALEUNIT));
    }];
}

#pragma mark ==========代理方法==========
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if ([self.delegate respondsToSelector:@selector(teachManagementSearchViewDelegateTextFieldEditWithContent:)]) {
        [self.delegate teachManagementSearchViewDelegateTextFieldEditWithContent:textField.text];
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{//过滤空格
    NSString *tem = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsJoinedByString:@""];
    if (![string isEqualToString:tem]) {
        return NO;
    }
    return YES;
}

#pragma mark ==========搜索==========
- (void)searchOpen:(UIButton *)btn{
    btn.userInteractionEnabled = NO;
    if ([self.delegate respondsToSelector:@selector(teachManagementSearchViewDelegateSearch:WithContent:)]) {
        [self.delegate teachManagementSearchViewDelegateSearch:btn WithContent:self.searchTextField.text];
    }
}



@end
